﻿using GalaSoft.MvvmLight;
using WP7Contrib.Services.Scheduling;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using System;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Controls;
using System.Linq;
using Microsoft.Phone.Tasks;

namespace CDFWPUG.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class pageMeetingViewModel : ViewModelBase
    {


        /// <summary>
        /// Initializes a new instance of the pageMeetingViewModel class.
        /// </summary>
        public pageMeetingViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                _meeting = Helpers.MeetingHelper.GetMeetingInfo(1);
            }
            else
            {



            }

            //TODO Need to add your bing maps api key here
            CredentialssProvider = new ApplicationIdCredentialsProvider("yourkeygoeshere"); 
            PinToStartCommand = new RelayCommand(HandlePinToStartCommand);
            ShareCommand = new RelayCommand(HandleShareCommand);
            LocationTaskCommand = new RelayCommand<SelectionChangedEventArgs>(HandleLocationTaskCommand);
        }

        private string reminderName;
        


        #region Properties
        private Model.Meeting _meeting;
        public Model.Meeting Meeting
        {
            get
            {
                return _meeting;
            }

            set
            {
                _meeting = value;
                RaisePropertyChanged("Meeting");
            }
        }

        private bool _IsReminderSet;
        public bool IsReminderSet
        {
            get
            {
                return _IsReminderSet;
            }

            set
            {
                _IsReminderSet = value;
                RaisePropertyChanged("IsReminderSet");

                if (_IsReminderSet)
                {
                    this.CreateReminder();
                }
                else
                {
                    ReminderManager.RemoveReminder(reminderName);
                }
            }
        }

        private string _MeetingID;
        public string MeetingID
        {
            get
            {
                return _MeetingID;
            }

            set
            {
                if (_MeetingID != value)
                {
                    _MeetingID = value;
                    RaisePropertyChanged("MeetingID");
                }

            }
        }

        private GeoCoordinate _MeetingLocation;
        public GeoCoordinate MeetingLocation
        {
            get
            {
                return _MeetingLocation;
            }

            set
            {
                _MeetingLocation = value;
                RaisePropertyChanged("MeetingLocation");

                
            }
        }

        private CredentialsProvider _CredentialsProvider;
        public CredentialsProvider CredentialssProvider
        {
            get
            {
                return _CredentialsProvider;
            }
            set
            {
                if (_CredentialsProvider != value)
                {
                    _CredentialsProvider = value;
                    RaisePropertyChanged("CredentialsProvider");
                }
            }
        }


        #endregion

        #region Commands

        public RelayCommand PinToStartCommand { get; set; }
        private void HandlePinToStartCommand()
        {
            Helpers.MeetingHelper.CreateSecondaryTile(this._meeting);
        }

        public RelayCommand ShareCommand { get; set; }
        private void HandleShareCommand()
        {
            string uri = string.Format("/Views/pageShare.xaml?Message={0}", Helpers.MeetingHelper.GetMeetingMessage(this.Meeting));
            SendNavigationRequestMessage(new Uri(uri, UriKind.Relative));
        }

        public RelayCommand<SelectionChangedEventArgs> LocationTaskCommand { get; set; }
        private void HandleLocationTaskCommand(SelectionChangedEventArgs args)
        {
            if (args.AddedItems.Count > 0)
            {
                ListBoxItem test = (ListBoxItem)args.AddedItems[0];

                switch (test.Name)
                {
                    case "itemPhone":
                        Helpers.FunctionsHelper.CreatePhoneCallTask(Meeting.MeetingVenue.Telephone, Meeting.MeetingVenue.Title);                       
                        break;
                    case "itemDirections":
                        Helpers.FunctionsHelper.CreateDirectionsTask(Meeting.MeetingVenue.Location, String.Format("{0},{1}",
                            Meeting.MeetingVenue.Town, Meeting.MeetingVenue.PostCode));
                        break;
                    case "itemWebsite":
                        Helpers.FunctionsHelper.CreateWebsiteTask(Meeting.MeetingVenue.Website);
                        break;
                    default:
                        break;
                }
            }
        }

        private void CreateReminder()
        {
            try
            {
                string reminderTitle= "WPUG Meeting"; 
                string reminderBody =string.Format("Reminder for the {0} meeting", Meeting.MeetingVenue.Town);
                DateTime reminderDate = Convert.ToDateTime(Meeting.Date +" "+  Meeting.Time);
                Uri reminderUri = new Uri(string.Format("/Views/pageMeeting.xaml?MeetingID={0}", Meeting.ID), UriKind.Relative);
                ReminderManager.AddReminder(reminderName, reminderTitle, reminderBody, reminderDate, reminderUri, Microsoft.Phone.Scheduler.RecurrenceInterval.None);
            }
            catch (Exception ex)
            {
                this.IsReminderSet = false;
            }
        }

        #endregion

        #region Navigation
        protected void SendNavigationRequestMessage(Uri uri)
        {
            Messenger.Default.Send<Uri>(uri, "NavigationRequest");
        }

        protected void SendMessageBoxRequest(String message)
        {
            Messenger.Default.Send<String>(message, "MessageBoxRequest");
        }
        #endregion


        public void LoadMeeting(int ID)
        {
            this.Meeting = Helpers.MeetingHelper.GetMeetingInfo(ID);
            reminderName= string.Format("{0} - {1}", Meeting.Date, Meeting.Title);
            IsReminderSet = ReminderManager.HasReminder(reminderName);
            MeetingLocation = this.Meeting.MeetingVenue.Location;
        }


    }
}